package fi.foyt.cs.persistence.dao.task;

import javax.persistence.EntityManager;

import fi.foyt.cs.persistence.dao.GenericDAO;
import fi.foyt.cs.persistence.domainmodel.task.TaskRequirements;

public class TaskRequirementsDAO extends GenericDAO<TaskRequirements> {

  public TaskRequirements create(Double money) {
    EntityManager entityManager = getEntityManager();

    entityManager.getTransaction().begin();
    try {
      TaskRequirements taskRequirements = new TaskRequirements();
      taskRequirements.setMoney(money);
      entityManager.persist(taskRequirements);
      entityManager.getTransaction().commit();
      return taskRequirements;
    } finally {
      if (entityManager.getTransaction().isActive())
        entityManager.getTransaction().rollback();
    }
  }
  
  public TaskRequirements updateMoney(TaskRequirements taskRequirements, Double money) {
    EntityManager entityManager = getEntityManager();

    entityManager.getTransaction().begin();
    try {
      taskRequirements.setMoney(money);
      taskRequirements = entityManager.merge(taskRequirements);
      entityManager.getTransaction().commit();
      return taskRequirements;
    } finally {
      if (entityManager.getTransaction().isActive())
        entityManager.getTransaction().rollback();
    }
  }

}
